package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Comment;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.RowMapper;

public class CommentDao {

	private DBHelp<Comment> db = new DBHelp<Comment>();
	
	public void save(Comment c) {
		String sql = "INSERT INTO t_comment(id,title,content,user_id,book_id) VALUES(?,?,?,?,?)";
		db.executeSQL(sql, c.getId(),c.getTitle(),c.getContent(),c.getUserId(),c.getBookId());
	}
	
	public void del(String id) {
		String sql = "delete from t_comment where id = ?";
		db.executeSQL(sql, id);
	}
	
	public List<Comment> findByBookId(String bookId){
		String sql = "select id,title,content,user_id,book_id,createtime from t_comment where book_id = ?";
		return db.executeQueryToList(sql, new CommentRowMapper(), bookId);
	}
	
	private class CommentRowMapper implements RowMapper<Comment> {

		public Comment mapRow(ResultSet rs) throws SQLException {
			Comment c = new Comment();
			c.setBookId(rs.getString("bookId"));
			c.setContent(rs.getString("content"));
			c.setCreatetime(rs.getString("createtime"));
			c.setId(rs.getString("id"));
			c.setTitle(rs.getString("title"));
			c.setUserId(rs.getString("userId"));
			return c;
		}
		
	}
}
